<{capture name="header"}>
<{css src="ome.css" app="ome"}>
<{/capture}>
<form method="post" id="iso_confirm" action="index.php?app=wms&ctl=admin_iostockorder&act=save_iso_confirm">
  <input type="hidden" name="io" value="<{$io}>" />
  <div class="tableform">
    <h3>入库单确认 - 传统方式</h3>
    <div class="division">
      按
      <select id="iostock_type">
        <option value="barcode">条码</option>
        <option value="bn">基础物料编码</option>
      </select>
      <input type="text" id="iostock_search" />
      <{button label="回车键确定" id="add_bn" app="desktop" icon="btn_add.gif"}>
      <div class="ome-stock" style="clear:both">
        <input type="hidden" name="iso_id" value="<{$iso_id}>" id="iso_id">
        <input type="hidden" name="type_id" value="<{$type_id}>">
        <input type="hidden" name="branch_id" value="<{$branch_id}>">
        <table class="gridlist">
          <thead>
            <tr>
              <!-- <th style="width:4%">选择</th> -->
              <th>基础物料名称</th>
              <!-- <th style="width:4%">规格</th> -->
              <th style="width:10%">基础物料编码</th>
              <th style="width:10%">条码</th>
              <th style="width:4%">单位</th>
              <th style="width:5%">调拨数量</th>
              <th style="width:6%" nowrap>已入库数量</th>
              <th style="width:5%" nowrap>入库数量</th>
              <th style="width:4%">新品</th>
              <th style="width:8%">操作</th>
              <!-- <th style="width:13%">商品备注</th> -->
            </tr>
          </thead>
          <tbody id="ome-stock-data">
            <{foreach from=$iso_items item=items}>
            <tr class="row" bn="<{$items.bn}>" barcode="<{$items.barcode}>" key="<{$items.iso_items_id}>">
              <td>
              <{if $items.entry_num!=0}>
                <input name="ids[]" type="hidden" value="<{$items.iso_items_id}>" <{if $items.entry_num!=0}>checked="checked"<{/if}> />
                <{/if}>
              <{$items.product_name}></td>
              <!-- <td><{$items.spec_info}></td> -->
              <td><{$items.bn}></td>
              <td><{$items.barcode}></td>
              <td><{$items.unit}></td>
              <td data-num="<{$items.nums}>"><{$items.nums}></td>
              <td data-in_num="<{$items.in_num}>"><{$items.in_num}></td>
              <td class="ome-input" id="bm_num_<{$items.product_id}>" onmouseover=visibility(event);>
                  <{if $items.use_expire == 1}>
                        <{if $items.entry_num!=0}>
                          0
                        <{else}>
                          -
                      <{/if}>
                  <{else}>
                  <{if $items.entry_num!=0}>
                      <input type="number" class="number_input" min="0" max="<{$items.entry_num}>" name="entry_num[<{$items.iso_items_id}>]" size="3" value="<{$items.entry_num}>" />
                      <{else}>
                          -
                  <{/if}>
              <{/if}>
              </td>
              <td class="isNew"><{if $items.is_new=="true"}>是<{else}>否<{/if}></td>
              <td>
              <{if $items.use_expire == 1}>
                    <{if $items.entry_num!=0}>
              <a class="instock_sl" bm_id="<{$items.product_id}>" expire_bm_info='' style="color:#0066cc; text-decoration:none;" id="expire_bm_<{$items.product_id}>">关联保质期</a><input name="is_expire_bn[]" type="hidden" value="<{$items.product_id}>" />
                    <{else}>
                        -
                    <{/if}>
              <{else}>-<{/if}>
              </td>
             <!--  <td class="ome-input">
                <{if $items.entry_num!=0}>
                <input type="text" name="item_memo[<{$items.iso_items_id}>]" value="<{$items.memo}>">
                <{/if}>
              </td> -->
            </tr>
            <input type="hidden" name="is_new[<{$items.iso_items_id}>]" value="<{$items.is_new}>">
            <{/foreach}>
          </tbody>
        </table>
        <div class="ome-stock-title ">共有<{$count}>件商品</div>
      </div>
      <div class="division">
        <table border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="5%">
              经办人:
            </td>
            <td>
              <input type="text" name="operator" vtype="required" value="<{$operator}>" size="8" />
            </td>
          </tr>
          <!-- <tr>
            <td>入库单备注: </td>
            <td>
              <textarea name="memo" rows="3" cols="60"></textarea>
            </td>
          </tr> -->
        </table>
      </div>

    </div>
  </div>
</form>
<{capture name='footbar'}>
<div class="table-action">
  <{button type="button" id="confirm_btn" label="确认入库" }>
  <{button type="button" id="close_btn" label="关闭页面" }>
</div>
<{/capture}>
<script>
  (function(){
    attachEsayCheck($('ome-stock-data'),'td:nth-child(first)');
   
    var dataNode = $('ome-stock-data');

	dataNode.getElements('.isNew').each(function(item,i){
		if(item.get('text').trim() == '是'){
			item.setStyles({'background-color':'#ffc',color:'red'});
			//item.getParent().getElement('input.ome-stock-store-input').value = '';
		}
	});
    var entry = $$('#ome-stock-data input[name^=entry_num[]');
    entry.addEvent('blur',function(){
	  this.value = (this.value.toInt()).limit(this.get('min'),this.get('max'));
      /*var num = this.getParent('tr').getElement('td:data-num').get('data-num');
      var innum = this.getParent('tr').getElement('td:data-in_num').get('data-in_num');
      if(this.value > num-innum) this.value = num-innum;
      else if(this.value <0) this.value =0;*/
    });

    //关闭页面
    $('close_btn').addEvent('click', function(){
      window.close();
    });

    $('iostock_search').addEvent('keyup',function(e){
      if(e.code!==13)return;
      var type=$('iostock_type').value,value=this.value,el;
      if (type=='barcode' && value==''){
        alert('请输入需定位的条形码');
        $('iostock_search').focus();
        return;
      }
      if (type=='bn' && value==''){
        alert('请输入需定位的货号');
        $('iostock_search').focus();
        return;
      }
      $$('#ome-stock-data tr').each(function(tr){
        if(tr.get(type)==value){el=tr;}
      });
      if(!el)return MessageBox.error('没找到相应数据');
      if(preel=this.retrieve('height_el'))preel.removeClass('select');
      el.addClass('select').getElement('input[type^=text]').focus();
      this.store('height_el',el);
    });
    $('add_bn').addEvent('click',function(){
      var type=$('iostock_type').value,el;
      var value=$('iostock_search').get('value');
      if (type=='barcode' && value==''){
        alert('请输入需定位的条形码');
        $('iostock_search').focus();
        return;
      }
      if (type=='bn' && value==''){
        alert('请输入需定位的货号');
        $('iostock_search').focus();
        return;
      }
      $$('#ome-stock-data tr').each(function(tr){
        if(tr.get(type)==value){el=tr;}
      });
      if(!el)return MessageBox.error('没找到相应数据');
      if(preel=this.retrieve('height_el'))preel.removeClass('select');
      el.addClass('select').getElement('input[type^=text]').focus();
      this.store('height_el',el);
    });
    $('iostock_search').focus();


    $$('#ome-stock-data input[type=text]').addEvent('keyup',function(e){
      if(this.getNext('.error')) this.getNext('.error').destroy();
      if(e.code!==13)return;
      var next_td=$(e.target).getParent('td').getNext('.ome-input');
      if(next_td&&next_td.getElement('input[type=text]')) return next_td.getElement('input[type=text]').focus();
      $('iostock_search').focus();
      if(preel=$('iostock_search').retrieve('height_el'))preel.removeClass('select');
    });

    $('iso_confirm').store('target',{
      onRequest:function(){
        $('confirm_btn').set('disabled', 'true');
        $('confirm_btn').getElements('span')[1].set('text','正在入库');
      },
      onComplete:function(jsontext){
        var json = JSON.decode(jsontext);
        if (typeof(json.error) != 'undefined'){
          dataNode.getElements('tr:key').removeClass('highlight');
         
          $('confirm_btn').set('disabled', false);
          $('confirm_btn').getElements('span')[1].set('text','开始入库');
        }else{
          $('confirm_btn').set('disabled', true);
          $('confirm_btn').getElements('span')[1].set('text','正在入库');
          opener.finderGroup['<{$env.get.find_id}>'].refresh.delay(100,opener.finderGroup['<{$env.get.find_id}>']);
          setTimeout('window.close()',200);
        }

      }
    });

    $('confirm_btn').addEvent('click',function(e){
		      var flag = [];
        entry.each(function(item,i){
            if(item.value < item.getParent('tr').getElement('td:data-num').get('data-num') -  item.getParent('tr').getElement('td:data-in_num').get('data-in_num')){
              flag.push(item.getParent('tr'));
            }
        });

        var value = '';
        $$('.instock_sl').each(function(item){
            if(item.getParent('td').getElement("input[name=expire_bm_info[]]")){
                item.getParent('td').getElement("input[name=expire_bm_info[]]").destroy();
            }
            value = item.get('expire_bm_info');
            new Element('input[type=hidden]', {'name': 'expire_bm_info[]', value: value}).inject(item.getParent('td'));
        });

        if(flag.length){
            if(confirm('入库数量和调拨数量不符，是否确认？')){
                $('iso_confirm').fireEvent('submit',e);
            }else{
                $$('#ome-stock-data tr.highlight').removeClass('highlight');
                flag.each(function(f,i){
                    f.addClass('highlight');
                    if(i===0) f.getElement('input[name^=entry_num[]').focus();
                });
            }
        }else{
            $('iso_confirm').fireEvent('submit',e);
        }
    });

        $$('.ome-input input, .ome-stock-store-input').addEvent('keyup',function(e){
            if(e.code==13) $('iostock_search').focus();
        });
    })();

    function visibility(e){
        var visiTips = new Tips({
            onShow:function(tip,el){
                el.addClass('active');
                tip.setStyle('display','block');
            },

            text: function(element){
                if(element.get('text').toInt() > 0){
                    var jsontext = element.getNext('td').getNext('td').getElement('a').get('expire_bm_info');
                    var ex_bm_info = Json.evaluate(jsontext);
                    var show_str = '';
                    if(ex_bm_info){
                        ex_bm_info.each(function(item){
                            show_str += '批次号：'+item.expire_bn+'，数量：'+item.in_num+'<br/>';
                        });
                    }
                    return show_str;
                }else{
                 return '';
                }
            }
        });

        var e  = new Event(e), el = e.target;
        if (el.get('text').toInt() > 0)
        {
            visiTips.attach(el);
            el.addEvent('mouseleave',function(){
                el.removeClass('active');
            });
            el.fireEvent('mouseenter',e);
        }
    }

    $$('.instock_sl').addEvent("click",function(){
        var iso_id = $('iso_id').value;
        var bm_id = this.get('bm_id');
        var has_expire_bm_info = this.get('expire_bm_info');
        new Dialog('index.php?app=wms&ctl=admin_iostockorder&act=storage_life_instock',{width:700,height:500,title:'关联保质期',ajaxoptions:{data:{iso_id:iso_id,bm_id:bm_id,has_expire_bm_info:has_expire_bm_info},method:'post'}});
    });
  </script>
